package com.example.aaaexemplos;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddActivity extends Activity implements CadastrarCallback {

	private EditText etName;
	private EditText etTelephone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.activity_add);

		this.etName = (EditText) this.findViewById(R.id.etName);
		this.etTelephone = (EditText) this.findViewById(R.id.etTelefone);

		Button btAdd = (Button) this.findViewById(R.id.btAdd);
		btAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AddActivity.this.onAddClick();
			}
		});
	}

	private void onAddClick() {
		String name = this.etName.getText().toString();
		String telephone = this.etTelephone.getText().toString();
		Person person = new Person(name, telephone);
		CadastrarTask cadastrarTask = new CadastrarTask(this, this, person);
		cadastrarTask.execute();
	}

	@Override
	public void onAddSuccess() {
		android.content.DialogInterface.OnClickListener okListener = new android.content.DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				AddActivity.this.finish();
				AddActivity.this.startActivity(new Intent(AddActivity.this,
						ListActivity.class));
			}
		};
		new AlertDialog.Builder(this).setTitle(R.string.app_name)
				.setMessage(R.string.addSuccess)
				.setPositiveButton(android.R.string.ok, okListener).create()
				.show();
	}

	@Override
	public void onAddFailure() {
		Toast.makeText(this, R.string.addFailure, Toast.LENGTH_LONG).show();
	}
}
